home *** CD-ROM | disk | FTP | other *** search
/ HTBasic 9.3 / HTBasic 9.3.iso / 83win / data1.cab / Basic_Plus_Examples / PIDCNTLR < prev    next >
Encoding:
Text File  |  2001-03-02  |  6.0 KB  |  149 lines

  1. 10    ! **************************************************************
  2. 20    ! Example: PID Controller
  3. 30    !
  4. 40    ! This example program simulates a PID Controller.
  5. 50    !
  6. 60    ! **************************************************************
  7. 70    !
  8. 80   DIM Points(3),Errors(64),Enum$(2)[10]
  9. 90    !
  10. 100   ! Construct the panel that controls the PID itself.
  11. 110   ! The panel indicates the current error and whether the loop is locked.
  12. 120   !
  13. 130  ASSIGN @Pid_panel TO WIDGET "PANEL";SET ("X":0,"Y":0,"WIDTH":400,"HEIGHT":245,"TITLE":"PID Controller","RESIZABLE":0,"MAXIMIZABLE":0)
  14. 140  CONTROL @Pid_panel;SET ("TITLE":" Example: PID Controller")
  15. 150  CONTROL @Pid_panel;SET ("SYSTEM MENU":"Quit")
  16. 160  ON EVENT @Pid_panel,"SYSTEM MENU" GOTO Finis
  17. 170   !
  18. 180   ! Slider to control Kp
  19. 190   !
  20. 200  ASSIGN @Kp_slider TO WIDGET "SLIDER";PARENT @Pid_panel,SET ("X":5,"Y":30,"WIDTH":70,"HEIGHT":180,"DECIMAL DIGITS":2)
  21. 210  CONTROL @Kp_slider;SET ("MINIMUM":0,"MAXIMUM":1,"MINOR INCREMENT":.01)
  22. 220  ASSIGN @Kp_note TO WIDGET "STRING";PARENT @Pid_panel,SET ("X":5,"Y":0,"WIDTH":70,"HEIGHT":20,"VALUE":"   Kp")
  23. 230   !
  24. 240   ! Slider to control Ki
  25. 250   !
  26. 260  ASSIGN @Ki_slider TO WIDGET "SLIDER";PARENT @Pid_panel,SET ("X":80,"Y":30,"WIDTH":70,"HEIGHT":180,"DECIMAL DIGITS":2)
  27. 270  CONTROL @Ki_slider;SET ("MINIMUM":0,"MAXIMUM":1,"MINOR INCREMENT":.01)
  28. 280  ASSIGN @Ki_note TO WIDGET "STRING";PARENT @Pid_panel,SET ("X":80,"Y":0,"WIDTH":70,"HEIGHT":20,"VALUE":"   Ki")
  29. 290   !
  30. 300   ! Slider to control Kd
  31. 310   !
  32. 320  ASSIGN @Kd_slider TO WIDGET "SLIDER";PARENT @Pid_panel,SET ("X":155,"Y":30,"WIDTH":70,"HEIGHT":180,"DECIMAL DIGITS":2)
  33. 330  CONTROL @Kd_slider;SET ("MINIMUM":0,"MAXIMUM":1,"MINOR INCREMENT":.01)
  34. 340  ASSIGN @Kd_note TO WIDGET "STRING";PARENT @Pid_panel,SET ("X":155,"Y":0,"WIDTH":70,"HEIGHT":20,"VALUE":"   Kd")
  35. 350   !
  36. 360   ! Meter that displays the error
  37. 370   !
  38. 380  ASSIGN @Error_meter TO WIDGET "METER";PARENT @Pid_panel,SET ("X":235,"Y":30,"WIDTH":150,"HEIGHT":140,"MINIMUM":-1,"MAXIMUM":1,"AUTOSCALE":0)
  39. 390  ASSIGN @Meter_note TO WIDGET "STRING";PARENT @Pid_panel,SET ("X":235,"Y":0,"WIDTH":150,"HEIGHT":20,"VALUE":"      Error")
  40. 400   !
  41. 410   ! String that displays whether the loop is locked or unlocked
  42. 420   !
  43. 430  ASSIGN @Lock_indicate TO WIDGET "STRING";PARENT @Pid_panel,SET ("X":270,"Y":180,"VALUE":"UNLOCKED","WIDTH":80,"HEIGHT":30,"BACKGROUND":2)
  44. 440   !
  45. 450   ! Slider that sets the set point for the loop
  46. 460   !
  47. 470  ASSIGN @Setpt_slider TO WIDGET "SLIDER";SET ("X":410,"Y":0,"WIDTH":150,"TITLE":"Set Point","DECIMAL DIGITS":2)
  48. 480  CONTROL @Setpt_slider;SET ("MINIMUM":0,"MAXIMUM":1,"MAJOR INCREMENT":.01,"MINOR INCREMENT":.1)
  49. 490   !
  50. 500   ! Strip chart to plot the set point and actual value
  51. 510   !
  52. 520  ASSIGN @Graph TO WIDGET "STRIPCHART";SET ("X":0,"Y":245,"TRACE COUNT":2,"SHARED X":1,"HEIGHT":235,"RESIZABLE":0,"MAXIMIZABLE":0)
  53. 530  CONTROL @Graph;SET ("CURRENT AXIS":"Y","ORIGIN":-.25,"RANGE":1.5)
  54. 540  CONTROL @Graph;SET ("CURRENT AXIS":"X","ORIGIN":0,"RANGE":100)
  55. 550  CONTROL @Graph;SET ("CURRENT TRACE":1,"TRACE LABEL":"Set point")
  56. 560  CONTROL @Graph;SET ("CURRENT TRACE":2,"TRACE LABEL":"Actual")
  57. 570  CONTROL @Graph;SET ("TITLE":" Set Point Value")
  58. 580   !
  59. 590   ! Cyclic control for the order of the load
  60. 600   !
  61. 610  Enum$(0)="1st"
  62. 620  Enum$(1)="2nd"
  63. 630  Load_order=1
  64. 640  ASSIGN @Load TO WIDGET "PUSHBUTTON";SET ("X":410,"Y":250,"WIDTH":150,"HEIGHT":50)
  65. 650  CONTROL @Load;SET ("TITLE":"Load Order","RESIZABLE":0,"MAXIMIZABLE":0,"LABEL":Enum$(Load_order))
  66. 660  ON EVENT @Load,"ACTIVATED",1 GOSUB Load_change
  67. 670   !
  68. 680   ! Initialize the sliders that control the PID
  69. 690   !
  70. 700  Ki=.17
  71. 710  CONTROL @Ki_slider;SET ("VALUE":Ki)
  72. 720  Kp=.4
  73. 730  CONTROL @Kp_slider;SET ("VALUE":Kp)
  74. 740  Kd=.37
  75. 750  CONTROL @Kd_slider;SET ("VALUE":Kd)
  76. 760   !
  77. 770   ! Initialize the setpoint slider
  78. 780   !
  79. 790  Setpt=.9
  80. 800  CONTROL @Setpt_slider;SET ("VALUE":Setpt)
  81. 810   !
  82. 820   ! Enable the event handling for the sliders
  83. 830   !
  84. 840  ON EVENT @Ki_slider,"DONE",2 GOSUB Ki_input
  85. 850  ON EVENT @Kp_slider,"DONE",2 GOSUB Kp_input
  86. 860  ON EVENT @Kd_slider,"DONE",2 GOSUB Kd_input
  87. 870  ON EVENT @Setpt_slider,"DONE",2 GOSUB Setpt_input
  88. 880   !
  89. 890   ! Initialize the actual value of the output and the array
  90. 900   ! that contains previous error values
  91. 910   !
  92. 920  Actual=0
  93. 930  FOR I=0 TO 62
  94. 940      Errors(I)=0
  95. 950  NEXT I
  96. 960  Sample=0
  97. 970  LOOP
  98. 980      Avg=0
  99. 990      FOR I=62 TO 0 STEP -1
  100. 1000         Avg=Avg+Errors(I)
  101. 1010         Errors(I+1)=Errors(I)
  102. 1020     NEXT I
  103. 1030     Errors(0)=Setpt-Actual
  104. 1040     Avg=(Avg+Errors(0))/64
  105. 1050     To_load=(Kp*Errors(0)+Ki*Avg+Kd*(Errors(0)-Errors(1)))
  106. 1060     SELECT Load_order
  107. 1070     CASE 0
  108. 1080    !
  109. 1090    ! First order load
  110. 1100    !
  111. 1110         Actual=(To_load*1.7)+Actual
  112. 1120     CASE 1
  113. 1130    !
  114. 1140    ! Second order load
  115. 1150    !
  116. 1160         Temp=To_load*5+Temp
  117. 1170         Actual=(Temp*.52+Actual)/2
  118. 1180     END SELECT
  119. 1190     Points(0)=Setpt
  120. 1200     Points(1)=Actual
  121. 1210     CONTROL @Graph;SET ("POINT LOCATION":Sample,"VALUES":Points(*))
  122. 1220     Sample=Sample+1
  123. 1230     CONTROL @Error_meter;SET ("VALUE":Errors(0))
  124. 1240     IF ABS(Errors(0))+ABS(Errors(1))<.02 THEN
  125. 1250         CONTROL @Lock_indicate;SET ("VALUE":"LOCKED","BACKGROUND":4)
  126. 1260     ELSE
  127. 1270         CONTROL @Lock_indicate;SET ("VALUE":"UNLOCKED","BACKGROUND":2)
  128. 1280     END IF
  129. 1290 END LOOP
  130. 1300 Ki_input: STATUS @Ki_slider;RETURN ("VALUE":Ki)
  131. 1310 Ki=PROUND(Ki,-2)
  132. 1320 RETURN
  133. 1330 Kp_input: STATUS @Kp_slider;RETURN ("VALUE":Kp)
  134. 1340 Kp=PROUND(Kp,-2)
  135. 1350 RETURN
  136. 1360 Kd_input: STATUS @Kd_slider;RETURN ("VALUE":Kd)
  137. 1370 Kd=PROUND(Kd,-2)
  138. 1380 RETURN
  139. 1390 Setpt_input: STATUS @Setpt_slider;RETURN ("VALUE":Setpt)
  140. 1400 Setpt=PROUND(Setpt,-2)
  141. 1410 RETURN
  142. 1420 Load_change: Load_order=Load_order+1
  143. 1430 IF Load_order>1 THEN Load_order=0
  144. 1440 CONTROL @Load;SET ("LABEL":Enum$(Load_order))
  145. 1450 RETURN
  146. 1460 Finis:  !
  147. 1470 ASSIGN @Pid_panel TO *          ! Delete PANEL widget
  148. 1480 END
  149.